<!DOCTYPE html>
<html lang="en" xmlns:shiro="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <link rel="stylesheet" href="/css/custom.form.css">
</head>
<body>
<div class="table_div">
    <div id="searchParam"  shiro:hasPermission="${entity?uncap_first}:list">
        <div class="layui-form-item">
            <div class="layui-input-inline">
                <input type="text" id="tableName" class="layui-input"  autocomplete="off" placeholder="请输入表名">
            </div>
            <div class="layui-input-inline ">
                <button class="layui-btn"   id="search">查询</button>
            </div>
        </div>

    </div>
    <table class="layui-table" id="showTable" lay-filter="showTable"></table>

</div>
<script type="text/html" id="tool">
    <a class="layui-btn layui-btn-xs" lay-event="add" shiro:hasPermission="sysGenerator:add">生成</a>
</script>
<script type="text/html" id="toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="batchAdd" shiro:hasPermission="sysGenerator:add">批量生成</button>
    </div>
</script>
</body>
</html>
<script src="/layui/layui.all.js"></script>
<script src="/js/core.util.js?v=1111"></script>
<script>
    var layer = layui.layer;
    var table = layui.table;
    var form = layui.form;
    var $ = layui.$;
    var token = CoreUtil.getData("access_token");


    //渲染table
    table.render({
        elem: '#showTable'
        , page: true //开启分页
        , url: '/sysGenerator/listByPage' //数据接口
        , method: "POST"
        , contentType: 'application/json;charset=utf8'
        , headers: {'authorization': token}
        , parseData: function (res) { //res 即为原始返回的数据
            // $(".layui-table-tool-temp").html("生成之前，请确保要生成的表已设置主键列为id；生成完成后，重启应用即可；")
            return {
                "code": res.code, //解析接口状态
                "msg": res.msg, //解析提示文本
                "count": res.data.total, //解析数据长度
                "data": res.data.records //解析数据列表
            };
        }
        , cols: [
            [ //表头
                {type: 'checkbox', fixed: 'left'},
                {field: 'tableName', title: '表名', width: 180, sort: true},
                {field: 'tableComment', title: '表备注', width: 150, sort: true},
                {field: 'createTime', title: '表创建时间', width: 180, sort: true},
                {field: 'genTime', title: '上次生成时间', width: 180, sort: true},
                {width: 100, toolbar: "#tool", title: '操作'}
            ]
        ]
        , toolbar: '#toolbar' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
        , id: 'tableId' //reload用
    });

    //刷新table
    var reloadTable = function () {
        var tableName = $('#tableName').val();
        //执行重载
        table.reload('tableId', {
            where: {
                "tableName": tableName
            }
        }, 'data');
    }
    //搜索
    $('#search').click(function () {
        reloadTable();
    })


    //列操作
    table.on('tool(showTable)', function (obj) {
        var data = obj.data;
        switch (obj.event) {
            case 'add':
                token = token.replace("#","%23");//转义token中的#号
                window.location.href = "/sysGenerator/add?authorization="+token+"&tables="+data.tableName;
                setTimeout(reloadTable, 1000)
                break;
        }
    });

    table.on('toolbar(showTable)', function(obj){
        switch(obj.event){
            case 'batchAdd':
                var checkStatus = table.checkStatus(obj.config.id);
                var data = checkStatus.data;
                if(data.length==0){
                    layer.msg("请选择要批量生成的表");
                }else {
                    var tables = [];
                    $(data).each(function (index,item) {
                        tables.push(item.tableName);
                    });
                    token = token.replace("#","%23");//转义token中的#号
                    window.location.href = "/sysGenerator/add?authorization="+token+"&tables="+tables.toString();
                    setTimeout(reloadTable, 1000)
                }
                break;
        };
    });

    //返回
    $("#btn_cancel").click(function () {
        $(".table_div").show();
        $(".operation").hide();
        return false;
    });

</script>